﻿using System.Collections.Generic;
using System.Linq;
using Webdiyer.WebControls.Mvc;

namespace NetSurveillance.WebApp.Exts
{
    public static class EnumableExtension
    {
        public static PagedList<T> ToPagedList<T>
          (
              this IEnumerable<T> allItems,
              int pageIndex,
              int pageSize
          )
        {
            if (allItems == null) return null;
            if (pageIndex < 1)
                pageIndex = 1;
            var itemIndex = (pageIndex - 1) * pageSize;
            var pageList = allItems as List<T> ?? allItems.ToList();
            var pageOfItems = pageList.Skip(itemIndex).Take(pageSize);
            var totalItemCount = pageList.Count();
            return new PagedList<T>(pageOfItems, pageIndex, pageSize, totalItemCount);
        }

        public static List<T> AsList<T>(this IEnumerable<T> enumerableList)
        {
            return enumerableList == null ? null : enumerableList.ToList();
        }
    }
}